package com.yonyou.gmmc.service.pointactivity.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

public class JsonHelper {
	private static final Logger logger = Logger.getLogger(JsonHelper.class);
	
	public static final String ACTIVITY_KEY = "data";

	/**
	 * Map转化为JSONObject
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static JSONObject map2JsonObject(Map<String, Object> map) throws Exception {

		JSONObject jsonObj = new JSONObject();

		if (map != null) {

			Iterator<String> it = map.keySet().iterator();

			while (it.hasNext()) {
				String key = it.next();
				jsonObj.put(key, map.get(key));
			}

		}

		return jsonObj;
	}

	/**
	 * JSONObject转化为Map
	 * 
	 * @param jsonObject
	 * @return
	 * @throws JSONException
	 * @throws Exception
	 */
	@SuppressWarnings("all")
	public static Map<String, Object> json2Map(JSONObject jsonObject) throws JSONException {

		String key;

		Object value;

		Iterator<String> keyIter = jsonObject.keys();

		Map<String, Object> map = new HashMap<String, Object>();

		while (keyIter.hasNext()) {
			key = (String) keyIter.next();
			value = jsonObject.get(key);
			map.put(key, value);
		}

		return map;
	}

	/**
	 * List<Map>转化为JSONArray
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public static JSONArray list2JsonArray(List<Map<String, Object>> list) throws Exception {
		JSONArray jsonArray = new JSONArray();

		if (list != null && list.size() > 0) {
			for (int i = 0, len = list.size(); i < len; i++) {
				Map<String, Object> map = list.get(i);
				JSONObject jsonObj = map2JsonObject(map);
				jsonArray.add(jsonObj);
			}
		}

		return jsonArray;
	}

	/**
	 * 字符串转json
	 * 
	 * @param str
	 * @return
	 * @throws JSONException
	 */
	public static JSONObject strToJson(String key, String value) throws JSONException {
		String s = "{\"" + key + "\":\"" + value + "\"}";
		System.out.println(s);
		JSONObject json = JSONObject.fromObject(s.toString());
		return json;
	}

	/**
	 * 字符串转json
	 * 
	 * @param str
	 * @return
	 * @throws JSONException
	 */
	public static JSONObject strToJson(String json) throws JSONException {
		return JSONObject.fromObject(json);
	}

	public static JSONArray beanToJsonList(List<?> list) {
		return JSONArray.fromObject(list);
	}
	
	public static String beanToJsonListForString(List<?> list) {
		return JSONArray.fromObject(list).toString();
	}
	
	public static String result2String(List<?> list) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		return JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String result2String(String json) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", json);
		return JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String result2String(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("data", map);
		return JSON.toJSONStringWithDateFormat(resultMap, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static JSONObject generateErrorResult(String error) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("errorMsg", error);
		
		logger.debug(error);

		return map2JsonObject(map);
	}

}
